// pages/addCar/addCar.js
import config from '../../config.js';
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {

    //车牌列表
    NoList: ["陕", "京", "津", "冀", "晋", "蒙", "辽", "吉", "黑", "沪", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂", "琼", "渝", "川", "黔", "滇", "藏", "甘", "青", "宁", "新", "台", "港", "澳"],
    NoIndex: 0,
    NoIndexTemp: 0,
    showProvince: false,

    brandShow: false,
    cateShow: false,
    typeShow: false,
    page: {
      start: 0,
      size: 20,
      refreshTime: '', // 刷新的时间 
      hideHeader: true,
      hideBottom: true,
      loadMoreData: '加载更多……'
    },
    brandId: undefined, //车型id,

    //车牌
    carNoPre: '陕',
    carNoNe: undefined,
    //车架号
    frameNumber: '',
    //发动机号
    engineNumber: undefined,

    //行驶证
    licenseFr: "https://app.longchg.com/gudongcar/images/licenseA.png",
    licenseBa: "https://app.longchg.com/gudongcar/images/licenseB.png",
    //身份证
    idenCardA: "https://app.longchg.com/gudongcar/images/idenCardA.png",
    idenCardB: "https://app.longchg.com/gudongcar/images/idenCardB.png",
    isUploadFr: false,
    isUploadBa: false,
    isUploadIDFr: false,
    isUploadIDBa: false,
    isUploadFirst: true,
    isUploadBaFirst: true,
    isUploadIDFirst: true,
    isUploadBaIDFirst: true,
  },


  //车牌选择
  NoChange: function(e) {
    let that = this;
    this.setData({
      NoIndex: e.detail.value,
      carNoPre: that.data.NoList[this.data.NoIndex]
    })
  },

  selectType: function(e) {
    let id = e.currentTarget.dataset.id,
      typeName = e.currentTarget.dataset.name,
      that = this;
    this.setData({
      brandId: id,
      typeName: typeName,
      typeShow: false
    })
  },



  carNoInputChange: function(e) {
    var value = e.detail.value;
    if (value != null && value.length < 7 ) {
      this.setData({
        carNoNe: e.detail.value.toUpperCase()
      })
    }
  },


  //车架号输入
  frameNumberInput: function(e) {
    this.setData({
      frameNumber: e.detail.value
    })
  },


  //发动机号输入
  engineNumberInput: function(e) {
    this.setData({
      engineNumber: e.detail.value
    })
  },


  submit: function() {

    var brandId = this.data.brandId,
      carNoNe = this.data.carNoNe,
      carNo = this.data.carNoPre + carNoNe,
      engineNumber = this.data.engineNumber,
      that = this;
    var isUploadFr = this.data.isUploadFr,
      isUploadBa = this.data.isUploadBa,
      licenseFr = this.data.licenseFr,
      licenseBa = this.data.licenseBa;

    console.log("车牌号长度" + carNoNe.length);
    if (!isUploadFr) {
      wx.showToast({
        title: '请选择行驶证正面',
        icon: 'none',
      })
      return;
    } else
    if (!isUploadBa) {
      wx.showToast({
        title: '请选择行驶证反面',
        icon: 'none',
      })
      return;
    } else if (!brandId) {
      wx.showToast({
        title: '请选择车辆品牌型号',
        icon: 'none',
      })
    } else if (!carNoNe) {
      wx.showToast({
        title: '请输入车牌号',
        icon: 'none',
      })
    } else if (carNoNe.length < 6) {
      wx.showToast({
        title: '请输入正确车牌号',
        icon: 'none',
      })
    } else if (!engineNumber) {
      wx.showToast({
        title: '请输入发动机号',
        icon: 'none',
      })
    } else if (engineNumber.length < 6) {
      wx.showToast({
        title: '请输入发动机号后6位',
        icon: 'none',
      })
    } else {
      wx.showLoading({
        title: '正在提交',
      })

      wx.request({
        url: base.addCar,
        header: {
          "Cookie": wx.getStorageSync("session"),
          'content-type': 'application/x-www-form-urlencoded',
        },
        data: {
          brandId: brandId,
          engineNumber: engineNumber,
          carNo: carNo,
          picA: licenseFr,
          picB: licenseBa
        },
        success: function(res) {
          console.log(res);
          wx.hideLoading();
          if (res.data.status == 200) {
            wx.setStorageSync('isAddCar', 1);
            wx.showToast({
              title: '车辆添加成功',
            })
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 500);
          } else {
            console.log(res);
            wx.showToast({
              title: res.data.message + '',
              icon: 'none'
            })
          }
        }

      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var date = new Date();
    this.setData({
      'page.refreshTime': date.toLocaleTimeString()
    })

  },
  // 上拉加载更多 
  loadMore: function() {
    var self = this;
    // 当前页是最后一页 
    if (self.data.currentPage == self.data.allPages) {
      self.setData({
        loadMoreData: '已经到顶'
      })
      return;
    }
    setTimeout(function() {
      console.log('上拉加载更多');
      var tempCurrentPage = self.data.page.start;
      tempCurrentPage = tempCurrentPage + 1;
      self.setData({
        currentPage: tempCurrentPage,
        hideBottom: false
      })
      self.getData();
    }, 300);
  },
  // 下拉刷新 
  refresh: function(e) {
    var self = this;
    setTimeout(function() {
      console.log('下拉刷新');
      var date = new Date();
      self.setData({
        'page.start': 0,
        'page.refreshTime': date.toLocaleTimeString(),
        'page.hideHeader': false
      })
      self.getData();
    }, 300);
  },

  getData: function() {
    wx.startPullDownRefresh({
      success: function(res) {
        console.log("下拉刷新完毕!");
      }

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var brand = wx.getStorageSync("brand");
    console.log(brand);
    if (brand != null && brand.carBrand!=null) {
      this.setData({
        brandId: brand.subBrandId,
        brandName: brand.carBrand.carBrandModel.name,
        typeName: brand.name
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    wx.setStorageSync("brand", null);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  selectBrandPre: function() {
    wx.navigateTo({
      url: './brandModelChooice',
    })
  },
  //行驶证
  uploadFr: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseFr: tempFilePaths,
          isUploadFr: true,
          isUploadFirst: false
        })

        console.log(that.data.licenseFr);


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: base.uploadPic,
          filePath: that.data.licenseFr[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadFr: true,
              })
              that.data.licenseFr = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBa: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseBa: tempFilePaths,
          isUploadBa: true,
          isUploadBaFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: base.uploadPic,
          filePath: that.data.licenseBa[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadBa: true,
              })
              that.data.licenseBa = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
  //身份证正面
  uploadFrID: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardA: tempFilePaths,
          isUploadIDFr: true,
          isUploadIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: base.uploadPic,
          filePath: that.data.idenCardA[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadIDFr: true,
                idenCardA: res.fileName
              })
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBaID: function() {
    let that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardB: tempFilePaths,
          isUploadIDBa: true,
          isUploadBaIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: base.uploadPic,
          filePath: that.data.idenCardB[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadIDBa: true,
                idenCardB: res.fileName
              })
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
  //省份选择
  provinceShow: function () {
    this.setData({
      showProvince: true,
    })
  },

  selectProvince: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      NoIndexTemp: index
    })
  },

  closeProvince: function () {
    this.setData({
      showProvince: false,
    })
  },

  // 车牌省份确定
  provinceSure: function () {
    var that = this;
    this.setData({
      showProvince: false,
      NoIndex: this.data.NoIndexTemp,
    }, function () {
      that.setData({
        carNoPre: that.data.NoList[that.data.NoIndex]
      })
    })
  },

})